package com.txsj.scan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.GoodsClassify;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/11/22
 * @Describe
 */
public class GoodsLeftAdapeter extends RecyclerView.Adapter<GoodsLeftAdapeter.ItemLeftRc> {
    private Context mcontext;
    private List<GoodsClassify> list;

    private int defItem = -1;
    private OnItemListener onItemListener;

    public GoodsLeftAdapeter(Context mcontext) {
        this.mcontext = mcontext;
        this.list = new ArrayList<>();
    }

    public void setSelect(int position) {
        this.defItem = position;
        notifyDataSetChanged();
    }

    public List<GoodsClassify> getList(){
        return list;
    }

    @Override
    public ItemLeftRc onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_left_rc, parent, false);
        return new ItemLeftRc(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(ItemLeftRc holder, int position) {
        holder.goodsLeftTvName.setText(list.get(position).getClass_name());

        if (defItem != -1) {
            if (defItem == position) {
                holder.goodsLeftTvName.setTextColor(Color.BLACK);
                holder.goodsLeftTvName.setBackgroundResource(R.color.white);
            } else {
                holder.goodsLeftTvName.setTextColor(R.color.color_9d);
                holder.goodsLeftTvName.setBackgroundResource(R.color.color_f4);
            }
        }else{
            if (0 == position) {
                holder.goodsLeftTvName.setTextColor(Color.BLACK);
                holder.goodsLeftTvName.setBackgroundResource(R.color.white);
            } else {
                holder.goodsLeftTvName.setTextColor(R.color.color_9d);
                holder.goodsLeftTvName.setBackgroundResource(R.color.color_f4);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size() == 0 ? 0 : list.size();
    }

    public void setData(List<GoodsClassify> goodsClassifyList) {
        this.list = goodsClassifyList;
        notifyDataSetChanged();
    }

    public class ItemLeftRc extends RecyclerView.ViewHolder {
        TextView goodsLeftTvName;
        public ItemLeftRc(View itemView) {
            super(itemView);
            goodsLeftTvName = itemView.findViewById(R.id.goods_left_tv_name);
            goodsLeftTvName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemListener != null) {
                        onItemListener.onClick(v,getLayoutPosition());
                    }
                }
            });
        }
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    public interface OnItemListener {
        void onClick(View v, int pos);
    }
}
